<?php

/**
 * @name user.php
 * @copyright (c) 2011
 */
App::uses('AuthComponent', 'Controller/Component');

class User extends AppModel
{

    var $name = 'User';

    /**
     * associations
     */
    var $hasOne = array(

    );

    var $hasMany = array(
        'LoginTrail' => array(
            'className' => 'LoginTrail',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'ContactGroup' => array(
            'className' => 'ContactGroup',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Contact' => array(
            'className' => 'Contact',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'SmsMeta' => array(
            'className' => 'SmsMeta',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'SmsOutbox' => array(
            'className' => 'SmsOutbox',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    var $belongsTo = array(
        'Group' => array(
            'className' => 'Group',
            'foreignKey' => 'group_id',
            'conditions' => '',
            'order' => '',
            'limit' => '',
            'dependent' => false
        )
    );



    /*public function beforeSave($options = array()) {
         if( isset($this->data[$this->alias]['password']) ) {
             $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
         }
         return true;
     }*/

    /**
     * This function retrieves all the users from the database tables users.
     * @name getAllUsers
     * @param void
     * @return mixed. Array containing the data retrieved.
     * @access public
     */
    function getAllUsers()
    {
        # fetch the data and return it.
        return $this->find('all', array('conditions' => array('User.deleted' => 'n'), 'recursive' => -1));
    }

    /**
     * This function retrieves the user data based upon his/her id
     * @name getUserById
     * @param string $userId string holding the user id.his parameter is defaulted to null.
     * When it is null it means that cake is going to use the ActiveRecord system to retrieve the data.
     * @return mixed. Array containing the user data.
     * @access public
     */
    function getUserById($userId = null, $recursive = 1)
    {
        $conditions = array(
            'User.id' => $userId,
            'User.deleted' => 'n'
        );
        # fetch the specific data from the server and retrun it.
        return $this->find('first', array('conditions' => $conditions, 'recursive' => $recursive));
    }

    # User Data manipulation functions
    /**
     * This function retrieves the user by his login name
     * @name getUserByLoginName
     * @param string $loginName
     * @return array of data containing the login name and the user password
     * @access public
     */

    function getUserByLoginName($loginName = null)
    {
        # search condition
        $conditions = array(
            'User.username' => $loginName,
            'User.deleted' => 'n'
        );
        # fetch the specific data from the server and retrun it.
        return $this->find('first', array('conditions' => $conditions, 'recursive' => 1));
    }


    function getUser($loginName = null)
    {
        # search condition
        $conditions = array(
            'User.username' => $loginName,
            'User.deleted' => 'n'
        );
        # fetch the specific data from the server and retrun it.
        return $this->find('first', array('conditions' => $conditions, 'recursive' => -1));
    }

    function getCreditBal($user_id = null){
        $conditions = array(
            'User.id' => $user_id
        );
        # fetch the specific data from the server and retrun it.
        $res = $this->find('first', array('fields'=>array('User.credit_bal'),'conditions' => $conditions, 'recursive' => -1));
        if($res){
            $bal = floatval($res['User']['credit_bal']);
        }
        else{
            $bal = 0;
        }
        return $bal;
    }


    function addCredit($user_id = null,$amt){
        $status = false;
        $conditions = array(
            'User.id' => $user_id
        );
        # fetch the specific data from the server and retrun it.
        $res = $this->find('first', array('fields'=>array('User.credit_bal'),'conditions' => $conditions, 'recursive' => -1));
        if($res){
            $bal = floatval($res['User']['credit_bal']);
            $new_bal = $bal + floatval($amt);
            $result= $this->updateCreditBal($user_id,$new_bal);
            $status = $result;
        }
        return $status;
    }

    function debitCredit($user_id = null,$amt){
        $status = false;
        $conditions = array(
            'User.id' => $user_id
        );
        # fetch the specific data from the server and retrun it.
        $res = $this->find('first', array('fields'=>array('User.credit_bal'),'conditions' => $conditions, 'recursive' => -1));
        if($res){
            $bal = floatval($res['User']['credit_bal']);
            $new_bal = $bal - floatval($amt);
            $result= $this->updateCreditBal($user_id,$new_bal);
            $status = $result;
        }
        return $status;
    }


    function updateCreditBal($user_id = null,$new_bal){
        $status = false;
        $conditions = array(
            'User.id' => $user_id
        );
        # fetch the specific data from the server and retrun it.
        $res = $this->find('first', array('fields'=>array('User.id'),'conditions' => $conditions, 'recursive' => -1));
        if($res){
            $result = $this->updateAll(
                array('User.credit_bal' => $new_bal),
                array('User.id' => $user_id)
            );
            if($result){
                $status = true;
            }
            else{
                $status = false;
            }
        }
        return $status;
    }

}